package com.online.sessionmanageserver.service;

import com.alibaba.fastjson.JSON;
import com.online.sessionmanageserver.domain.User;
import com.online.sessionmanageserver.domain.WeChatUserInfo;
import com.online.sessionmanageserver.domain.WeChatUserInfoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: liuqing
 * @Despriction:
 * @Date:Created in 2018/4/24 17:02
 * @Modify By:
 */
@Repository
public class RedisService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    final static String AGENT = "-AGENT";

    public Map<Object, Object> getAgentSessions(String aId) {
        return stringRedisTemplate.opsForHash().entries(aId + AGENT);
    }

    public void addSet(String keyOfCId, String aId) {
        stringRedisTemplate.opsForSet().add(keyOfCId, aId);
    }

    public void delSet(String keyOfCId, String aId) {
        stringRedisTemplate.opsForSet().remove(keyOfCId, aId);
    }

    public void add(String key, String str, Long time) {
        stringRedisTemplate.opsForValue().set(key, str, time, TimeUnit.MINUTES);
    }

    public void expire(String key, Long time) {
        stringRedisTemplate.expire(key, time, TimeUnit.MINUTES);
    }

    public void add(String key, String str) {
        stringRedisTemplate.opsForValue().set(key, str);
    }

    public String getStr(String key) {
        String source = stringRedisTemplate.opsForValue().get(key);
        System.out.println("source:" + source);
        if (!StringUtils.isEmpty(source)) {
            return source;
        }
        return null;
    }

    public List<String> getMessageList(String sessionId) {
        List<String> msgList = stringRedisTemplate.opsForList().range(sessionId, 0, -1);
        if (msgList != null) {
            stringRedisTemplate.opsForList().trim(sessionId, msgList.size(), -1);
        }
        return msgList;
    }

    public WeChatUserInfo getWeChatUserInfo(String key) {
        String source = stringRedisTemplate.opsForValue().get(key);
        System.out.println("source:" + source);
        if (!StringUtils.isEmpty(source)) {
            return JSON.parseObject(source, WeChatUserInfo.class);
        }
        return null;
    }

    public List<WeChatUserInfoSession> getWeChatUserInfo(Map<Object, Object> agentSessionsMap) {
        Collection<String> collection = new ArrayList<>();
        Iterator<Object> it = agentSessionsMap.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            collection.add(key);
        }

        List<String> weChatUserList = stringRedisTemplate.opsForValue().multiGet(collection);
        if (weChatUserList != null && weChatUserList.size() != 0) {
            List<WeChatUserInfoSession> wcuis = new ArrayList<>();
            for (int i = 0; i < weChatUserList.size(); i++) {
                WeChatUserInfo weChatUserInfo = JSON.parseObject(weChatUserList.get(i), WeChatUserInfo.class);
                if (!StringUtils.isEmpty(weChatUserInfo)){
                    WeChatUserInfoSession weChatUserInfoSession = new WeChatUserInfoSession(weChatUserInfo.getOpenId(),
                            weChatUserInfo.getName(),
                            weChatUserInfo.getAddress(),
                            weChatUserInfo.geteId(),
                            weChatUserInfo.getSex(),
                            Integer.parseInt((String) agentSessionsMap.get(weChatUserInfo.getOpenId())));
                    wcuis.add(weChatUserInfoSession);
                }
            }
            return wcuis;
        }

        return null;
    }


    public boolean delete(String key) {
        return stringRedisTemplate.delete(key);
    }

}
